package com.we.flink.dcapiuserlabel.tag;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.dcapiuserlabel.table.TableHfqMarketLoan;

public class TagHfqMarketLoan {
    public long uid;
    public int dc_submit_loan_time;

    public TagHfqMarketLoan(long uid, int dc_submit_loan_time) {
        this.uid = uid;
        this.dc_submit_loan_time = dc_submit_loan_time;
    }

    public static TagHfqMarketLoan init(long uid) {
        return new TagHfqMarketLoan(uid, 0);
    }

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableHfqMarketLoan.QUERYSQL);
        DataStream<Tuple2<Boolean, TableHfqMarketLoan>> inputStream =
                tableEnv.toRetractStream(tableRes, TableHfqMarketLoan.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableHfqMarketLoan>>() {
                                    @Override
                                    public boolean filter(Tuple2<Boolean, TableHfqMarketLoan> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableHfqMarketLoan>, Long>() {
                                    @Override
                                    public Long getKey(Tuple2<Boolean, TableHfqMarketLoan> value)
                                            throws Exception {
                                        return value.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<
                                        Tuple2<Boolean, TableHfqMarketLoan>, String>() {
                                    ValueState<TagHfqMarketLoan> hfqMarketLoanTagVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        hfqMarketLoanTagVState =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<
                                                                        TagHfqMarketLoan>(
                                                                        "HfqMarketLoanTagVState",
                                                                        TagHfqMarketLoan.class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableHfqMarketLoan> value,
                                            Collector<String> out)
                                            throws Exception {
                                        long uid = value.f1.getUid();
                                        int borrow_time = value.f1.getBorrow_time();

                                        TagHfqMarketLoan tmpState = hfqMarketLoanTagVState.value();
                                        TagHfqMarketLoan curState =
                                                tmpState == null
                                                        ? TagHfqMarketLoan.init(uid)
                                                        : tmpState;

                                        if (curState.dc_submit_loan_time < borrow_time) {
                                            JSONObject jsonObject = new JSONObject();
                                            jsonObject.put("uid", uid);
                                            jsonObject.put("dc_submit_loan_time", borrow_time);
                                            out.collect(jsonObject.toString());

                                            curState.dc_submit_loan_time = borrow_time;
                                            hfqMarketLoanTagVState.update(curState);
                                        }
                                    }
                                });

        return out;
    }

    public long getUid() {
        return uid;
    }

    public int getDc_submit_loan_time() {
        return dc_submit_loan_time;
    }
}
